﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Meqs.Logic.Interface;
using Meqs.Entity;

public partial class Credit_Teacher_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    /// <summary>
    /// 能否使用学分设置
    /// </summary>
    protected bool canUseManage = false;
    /// <summary>
    /// 能否使用学分录入
    /// </summary>
    protected bool canUseInput = false;

    protected bool canStart = false;
    protected int GetStartInfor()
    {

        SchoolCommon.FirstClassEval();
        SchoolCommon.FirstCredit();
        SchoolCommon.FirstMoral();
        SchoolCommon.FirstTeacherEval();

        //FirstInit();

        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", Common.GetConstSchoolMark());
        //查询学校信息表，获取该校学期值
        List<SchoolInfo> listSchoolInfo = (List<SchoolInfo>)Container<ISchoolInfoLogic>.Instance.FindAll(ht);

        IStuCauseEvalHistoryLogic eduUserLogic = Container<IStuCauseEvalHistoryLogic>.Instance;
        ht = new Hashtable();
        ht.Add("SchoolYear", listSchoolInfo[0].SchoolYear);
        ht.Add("Term", listSchoolInfo[0].SchoolTerm);
        ht.Add("Status", 1);
        ht.Add("SchoolMark", CommonScore.GetSchoolMark);
        int num = eduUserLogic.FindCount(ht);
        //canUseManage = CommonScore.ManagerCredit;
        canUseManage = SchoolCommon.CheckRole((int)RoleKeyEnum.Teach_Director);
        if (num > 0)
        {
            canStart = true;
            //已经确认所有规则
            if (canUseManage)
            {
                //有管理权限的
                return 2;
            }
            else
            {
                //没有管理权限
                ht = new Hashtable();
                ht.Add("TeacherID", CommonScore.GetUserID);
                if (Container<IStuCauseEvalHistoryLogic>.Instance.FindTeacherCount(ht) > 0)
                {
                    //并且是任课老师
                    return 3;
                }
            }
        }
        else
        { 
            //未确认规则
            if (canUseManage)
            {
                //有管理权限的
                return 1;
            }

        }
        //Container<ISchoolInfoLogic>.Instance.FindAll(ht);
        return 0;
    }

    private void FirstInit()
    {
        SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
        
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", si.SchoolMark);

        IList<CauseInfo> cis = Container<ICauseInfoLogic>.Instance.FindAll(ht);

        ht = new Hashtable();
        ht.Add("SchoolYear", si.SchoolYear);
        ht.Add("Term", si.SchoolTerm);
        ht.Add("SchoolMark", si.SchoolMark);
        IStuCauseEvalHistoryLogic eduUserLogic = Container<IStuCauseEvalHistoryLogic>.Instance;
        IList<StuCauseEvalHistory> stuhis = eduUserLogic.FindAll(ht);


        int lastYear = si.SchoolYear;
        int lastTerm = si.SchoolTerm;
        GetLastTerm(ref lastYear, ref lastTerm);
        //去年的历史
        ht = new Hashtable();
        ht.Add("SchoolYear", lastYear);
        ht.Add("Term", lastTerm);
        ht.Add("SchoolMark", si.SchoolMark);
        IList<StuCauseEvalHistory> lasthistory = Container<IStuCauseEvalHistoryLogic>.Instance.FindAll(ht);
        IList<StuCauseEvalType> lasttype = null;
        IList<StuCauseEvalItem> lastitems = null;
        foreach(CauseInfo c in cis)
        {
            if (!Find(c.RID, stuhis))
            {
                int oldhistoryid = GetLastYearID(si.RID, lasthistory);
                if (oldhistoryid != 0)
                {
                    //填历史
                    StuCauseEvalHistory seh = new StuCauseEvalHistory(0, si.SchoolYear, si.SchoolTerm, c.RID, 0, 0);
                    seh.SchoolMark = si.SchoolMark;
                    int newhistoryid = Container<IStuCauseEvalHistoryLogic>.Instance.Create(seh);

                    if (lasttype == null)
                    {
                        ht = new Hashtable();
                        ht.Add("HistoryID", oldhistoryid);
                        ht.Add("SchoolMark", si.SchoolMark);
                        lasttype = Container<IStuCauseEvalTypeLogic>.Instance.FindAll(ht); ;
                    }
                    if (lastitems == null)
                    {
                        ht = new Hashtable();
                        ht.Add("HistoryID", oldhistoryid);
                        ht.Add("SchoolMark", si.SchoolMark);
                        lastitems = Container<IStuCauseEvalItemLogic>.Instance.FindAll(ht); ;
                    }
                    foreach (StuCauseEvalType t in lasttype)
                    {
                        if (t.CauseID == c.RID)
                        {
                            int oldtypeid = t.RID;
                            t.HistoryID = newhistoryid;
                            t.SchoolMark = si.SchoolMark;
                            //填类别
                            int newtypeid = Container<IStuCauseEvalTypeLogic>.Instance.Create(t);
                            foreach (StuCauseEvalItem i in lastitems)
                            {
                                if (t.CauseID == c.RID && oldtypeid == i.TypeID)
                                {
                                    i.TypeID = newtypeid;
                                    i.HistoryID = newhistoryid;
                                    i.SchoolMark = si.SchoolMark;
                                    //填题目
                                    Container<IStuCauseEvalItemLogic>.Instance.Create(i);
                                }
                            }
                        }
                    }
                }
                else
                {
                    StuCauseEvalHistory seh = new StuCauseEvalHistory(0, si.SchoolYear, si.SchoolTerm, c.RID, 0, 0);
                    seh.SchoolMark = si.SchoolMark;
                    int newhistoryid = Container<IStuCauseEvalHistoryLogic>.Instance.Create(seh);
                }
            }
        }

        //级别
        ht = new Hashtable();
        ht.Add("SchoolYear", lastYear);
        ht.Add("Term", lastTerm);
        ht.Add("SchoolMark", si.SchoolMark);
        IList<StuCauseLevel> lastlevel = Container<IStuCauseLevelLogic>.Instance.FindAll(ht);
        foreach(StuCauseLevel scl in lastlevel)
        {
            scl.SchoolYear = si.SchoolYear;
            scl.Term = si.SchoolTerm;
            scl.SchoolMark = si.SchoolMark;
            Container<IStuCauseLevelLogic>.Instance.Create(scl);
        }
    }
    private int GetLastYearID(int causeid, IList<StuCauseEvalHistory> lasthistory)
    { 
        foreach(StuCauseEvalHistory s in lasthistory)
        {
            if (s.CauseID == causeid)
                return s.RID;
        }
        return 0;
    }
    private void GetLastTerm(ref int schoolyear,ref int term)
    {
        if (term == 2)
        {
            //当前是上学期
            term = 1;
        }
        else if(term == 1)
        {
            schoolyear = schoolyear - 1;
            term = 2;
        }
    }
    private bool Find(int id, IList<StuCauseEvalHistory> stuhis)
    { 
        foreach(StuCauseEvalHistory s in stuhis)
        {
            if (s.CauseID == id)
            {
                return true;
            }
        }
        return false;
    }
    protected string GetNowYearAndTerm
    {
        get
        {
            SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
            return Common.ConvertYearAndTerm(si.SchoolYear, si.SchoolTerm);
        }
    }
    /// <summary>
    /// 修改密码
    /// </summary>
    /// <param name="schoolMark"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int UpdatePwd(string oldPwd, string newPwd)
    {
        //局级

        if (Common.GetUserInfo().UserType == 1 || Common.GetUserInfo().UserType == 2)
        {
            IEducationUsersLogic eduUserLogic = Container<IEducationUsersLogic>.Instance;

            Hashtable ht = new Hashtable();
            ht.Add("RID", Common.GetUserInfo().UID);
            ht.Add("Passwd", oldPwd);
            int iCount = eduUserLogic.FindCount(ht);

            if (iCount == 0) return -1;          //原密码不正确

            //重置密码
            List<int> list = new List<int>();
            list.Add(Common.GetUserInfo().UID);
            return eduUserLogic.ResetPwd(newPwd, list);
        }

        //校级
        if (Common.GetUserInfo().UserType == 3 || Common.GetUserInfo().UserType == 4)
        {
            ITeacherInfoLogic teacherLogic = Container<ITeacherInfoLogic>.Instance;

            Hashtable ht = new Hashtable();
            ht.Add("RID", Common.GetUserInfo().UID);
            ht.Add("Passwd", oldPwd);
            int iCount = teacherLogic.FindCount(ht);

            if (iCount == 0) return -1;          //原密码不正确

            //重置密码
            List<int> list = new List<int>();
            list.Add(Common.GetUserInfo().UID);
            return teacherLogic.ResetPassword(newPwd, list);
        }

        return 0;

    }
}
